package com.koron.common.web.servlet;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.RedisUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.swan.bean.MessageBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

@Controller
public class LoginOutAction {

	/**
	 * 默认域名 scheme 为 https
	 */
	private String scheme = Constant.HTTPS;

	@RequestMapping(value = "/loginout.api", method = RequestMethod.GET)
	@ResponseBody
	public MessageBean<?> loginout(String ticket, HttpServletRequest request, HttpServletResponse response) throws IOException {
		MessageBean<String> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", String.class);
		if(StringUtils.isNotEmpty(ticket)) {
			UserInfoBean userInfo = Tools.getLoginBean(ticket);
			RedisUtils.del(ticket);
			RedisUtils.del("user_auth_" + ticket + "_" + userInfo.getUserInfo().getAcount());
			RedisUtils.del("user_company_" + ticket + "_" + userInfo.getUserInfo().getAcount());
			RedisUtils.del("alloperation");
			RedisUtils.del("orgalloperation");
		}
		// 假如是开发环境 则域名的 scheme 为 http
		if (Objects.equals("dev", ApplicationConfig.getEnv())){
			scheme = Constant.HTTP;
		}
		String host = scheme + "://" + request.getHeader("Host") + request.getContextPath();
		// UMA 退出登录接口地址
		if (java.util.Objects.equals(Constant.UMA_SSO_PLAT, ApplicationConfig.getSsoPlatform())){
			info.setData(ApplicationConfig.getLoginOutUrl() + host);
		}
		// 竹云退出登录接口地址
		if (Objects.equals(Constant.IAM_SSO_PLAT, ApplicationConfig.getSsoPlatform())){
			info.setData(ApplicationConfig.getIamLoginoutUrl() + host);
		}
		return info;
	}
}
